﻿using System;
using System.Linq;
using System.Globalization;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.Services;
using QuanLyThongTinCoSo.Common;
using ComputerVision.NganhForm;
using ComputerVision.NguyenNhanChayForm;


namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmTinhHinhChayNo : frmDetail
    {
        public Guid IdPhieu;
        private readonly IService<TinhHinhChayNo> _tinhHinhChayNoService;
        private readonly IService<NguyenNhanChay> _nguyenNhanChayNoService;

        public frmTinhHinhChayNo()
        {
            InitializeComponent();
            _tinhHinhChayNoService = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            _nguyenNhanChayNoService = Program.Container.Resolve<IService<NguyenNhanChay>>();
            Text = @"Thêm mới tình hình cháy nổ";
            dtNgay.Focus();
            LoadNguyenNhanChay();
        }

        public frmTinhHinhChayNo(Guid id)
        {
            InitializeComponent();
            _tinhHinhChayNoService = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            _nguyenNhanChayNoService = Program.Container.Resolve<IService<NguyenNhanChay>>();
            Text = @"Sửa tình hình cháy nổ";
            dtNgay.Focus();
            LoadNguyenNhanChay();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _tinhHinhChayNoService.Detail(id);
            if (item != null)
            {
                dtNgay.Value = item.Ngay.Value;
                txtNoiPhatSinhChayNo.Text = item.NoiPhatSinhChayNo;
                txtNguyenNhan.Text = item.NguyenNhan;
                txtChet.Value = item.Chet.Value;
                txtBiThuong.Value = item.BiThuong.Value;
                txtXuLyHanhChinh.Text = item.XuLyHanhChinh.ToString();
                txtKhoiTo.Text = item.KhoiToVuAn.ToString();
                txtTaiSan.Text = item.TaiSan.ToString();

                if(item.NguyenNhanChayId.HasValue)
                {
                    int index = 0;
                    foreach(var temp in drlNguyenNhan.Items)
                    {
                        var nguyenNhan = temp as NguyenNhanChay;
                        if (nguyenNhan != null)
                        {
                            if (nguyenNhan.Id == item.NguyenNhanChayId.Value)
                            {
                                drlNguyenNhan.SelectedIndex = index;
                                break;
                            }
                        }
                        index++;
                    }
                }
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new TinhHinhChayNo
                {
                    Id = Guid.NewGuid(),
                    Ngay = dtNgay.Value.Date,
                    NoiPhatSinhChayNo = txtNoiPhatSinhChayNo.Text,
                    NguyenNhan = txtNguyenNhan.Text,
                    Chet = (int?) txtChet.Value,
                    BiThuong = (int?) txtBiThuong.Value,
                    TaiSan = decimal.Parse(txtTaiSan.Text),
                    XuLyHanhChinh = txtXuLyHanhChinh.Text,
                    KhoiToVuAn = txtKhoiTo.Text,
                    PhieuDieuTraCoSoId = IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser,
                    NguyenNhanChayId = Guid.Parse(drlNguyenNhan.SelectedValue.ToString())
                };

                _tinhHinhChayNoService.Create(model);

            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _tinhHinhChayNoService.Detail(guid);
            if (item != null)
            {
                item.Ngay = dtNgay.Value.Date;
                item.NoiPhatSinhChayNo = txtNoiPhatSinhChayNo.Text;
                item.NguyenNhan = txtNguyenNhan.Text;
                item.Chet = (int?) txtChet.Value;
                item.BiThuong = (int?) txtBiThuong.Value;
                item.TaiSan = decimal.Parse(txtTaiSan.Text);
                item.XuLyHanhChinh = txtXuLyHanhChinh.Text;
                item.KhoiToVuAn = txtKhoiTo.Text;
                item.NguyenNhanChayId = Guid.Parse(drlNguyenNhan.SelectedValue.ToString());
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _tinhHinhChayNoService.Update(item);
            }
        }

        #endregion

        private void LoadNguyenNhanChay()
        {
            drlNguyenNhan.DataSource = _nguyenNhanChayNoService.ToList().OrderBy(l => l.NguyenNhan).ToList();
            drlNguyenNhan.DisplayMember = "NguyenNhan";
            drlNguyenNhan.ValueMember = "Id";
        }

        private void txtTaiSan_TextChanged(object sender, EventArgs e)
        {
            string value = txtTaiSan.Text.Replace(",", "").Replace("$", "");
            decimal ul;
            if (decimal.TryParse(value, out ul))
            {
                txtTaiSan.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:N0}", ul);
            }
            txtTaiSan.SelectionStart = txtTaiSan.Text.Length + 1;
        }

        private void AddNguyenNhanChay(object sender, EventArgs e)
        {
            using (var frm = new frmNguyenNhanChayDetail())
            {
                frm.OnLoadData += LoadNguyenNhanChay;
                frm.ShowDialog();
            }
        }
    }
}
